"""metadata - utilities for extracting and transforming metadata models to and from different common standard representations. With a focus on metadata standards for geographic information, but customisable for other domains."""

"""Abstract model for common metadata classes and properties"""

from django.template import loader, Context
import elementtree.ElementTree as etree
from urllib2 import urlopen

class Metadata:
    def __init__(self):
        self.metadata = []
    @property
    def dataset(self):
        return self.metadata[0]
    @property
    def datasource(self):
        if len(self.metadata) > 1:
            return self.metadata[1]
        
    @property
    def agent(self):
        if len(self.metadata) > 2:
            return self.metadata[2]
    @property
    def timespan(self):
        if len(self.metadata) > 3:
            return self.metadata[3]

    @property
    def extents(self):
        return self.dataset.extents        
 
    @property
    def title(self):
        """return the reported title of this data source"""
        return self.dataset.title

    @property
    def keywords(self):
        """return a list of keyword,language tuples associated
                with this data source
                """
        return map(lambda x: (x, 'en'), self.dataset.keywords)
                
    @property
    def abstract(self):
        """return the reported abstract for this data source"""
        return self.dataset.abstract

    def get_contacts(self):
        pass

    def get_layers(self):
        pass


class A(object):
    """Base class for abstracted objects."""
    def __getattr__(self, attr):
        return self.__dict__.get(attr, None)

class Agent(A):
    """An Agent - a person or organisation - having some relationship with a data set.
    >>> a = Agent()     
"""
    pass

class Dataset(A):
    """A Dataset - an abstract representation of a dataset, e.g. those properties of it which are not particular to a specific instance of it. E.g. creator, spatial extents, licensing information.
    >>> d = Dataset()
"""
    pass

class Datasource(A):
    """A Datasource - where the contents of a dataset can be seen. May be a file on a local or remote system, or a network service interface.
    >>> d = Datasource() 
    """
    pass

class Timespan(A):
    """A Timespan - applicable to the contents of a Dataset. May consist of a single date/timestamp or a start and end date range.
    >>> t = Timespan()
    """
    pass

class License(A):
    """A License - terms applicable to the re-use and redistribution of a Dataset. Ought to have a URL.
    l = License()
    """
    pass

def set(obj,property,node):
    """Utility method. Use when setting a property on one of these 
    objects via an elementtree node which may not exist.
    >>> a = Agent()
    >>> set(a,'email',None)
    False
    >>> node = A()
    >>> node.text = 'test@frot.org'
    >>> set(a,'email',node)
    >>> a.email
    'test@frot.org'
    """
    if node is not None:
        try:
            obj.__dict__[property] = node.text
        except:
            return False
    else:
        return False

def _test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    _test()

